﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;
namespace zichan.BLL
{
    public class ServerInfoService
    {
        public static string Add(ServerInfo model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    db.ServerInfos.Add(model);
                    db.SaveChanges();
                    return string.Empty;
                }
                catch(Exception ex)
                {
                    return "无法完善信息";
                }
            }
        }
        public static ServerInfo Get(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.ServerInfos.FirstOrDefault(o => o.AssetId == id);
            }
        }
        public static string Edit(ServerInfo model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return string.Empty;
            }
        }
        public static int Get(string code)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                int assetId = db.Assets.Where(o=>o.Code==code).Select(o=>o.Id).FirstOrDefault();
                return db.ServerInfos.Where(o => o.AssetId == assetId).Select(o => o.U_Number).FirstOrDefault();
            }
        }

    }
}
